<template>
<div class="home-container">
    <div class="page-nav-bar" @click="$router.push('/search')">
      <van-button icon="search" type="primary" round>搜索</van-button>
    </div>

    <van-tabs v-model="active" animated swipeable>
      <van-tab :title="item.name" v-for="item in channels" :key="item.id">
        <article-list :channel="item"></article-list>
      </van-tab>
      <template slot="nav-right">
        <div class="more" @click="show=true">
          <i class="toutiao toutiao-gengduo"></i>
        </div>
        <div class="more1"></div>
      </template>
    </van-tabs>

    <!-- 弹出层 -->
    <van-popup
      v-model="show"
      closeable
      close-icon="close"
      position="bottom"
      :style="{ height: '100%' }"
    >
    <channel-edit :myChannels="channels" :active="active" @update-active="updateActive"></channel-edit>
    </van-popup>
</div>
</template>

<script>
// 在单文件组件中子组件用法：
// 第一步、先import导入
// 第二步、在components中注册一下
// 第三步、在template中可以使用
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  // 注册子组件
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      channels: [],
      show: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.getUserChannels()
  },
  mounted () {},
  methods: {
    updateActive (active, flag) {
      this.active = active
      if (!flag) {
        this.show = false
      }
    },
    async getUserChannels () {
      // 如果用户登录了，获取用户自己的频道，如果没有登录，首先会看一下本地存储，如果有，就用本地存储，如果没有，就默认频道
      let channels = []
      if (this.user) {
        const { data } = await getUserChannels()
        channels = data.data.channels
      } else {
        const localChannels = getItem('TOUTIAO_CHANNELS')
        if (localChannels) {
          channels = localChannels
        } else {
          const { data } = await getUserChannels()
          channels = data.data.channels
        }
      }
      this.channels = channels
    }
  }
}
</script>

<style scoped lang="less">
.page-nav-bar{
  position: fixed;
  top:0;
  left:0;
  right:0;
  z-index: 1;
  background-color: #3296fa;
  height:92px;
  display: flex;
  justify-content: center;
  align-items: center;
  .van-button{
    width:554px;
    height:64px;
    background-color: #5babfb;
    border:none;
  }
}
/deep/ .van-tabs__wrap{
  position: fixed;
  top:92px;
  left:0;
  right:0;
  z-index:1;
}
.home-container{
  padding-top:174px;
}

/deep/ .van-tab{
  border-right: 1px solid #ccc;
  min-width: 200px;
  height:82px;
}
/deep/ .van-tabs__line{
  width: 30px;
  height: 6px;
  background-color: #5babfb;
}

.more{
  width:66px;
  height:80px;
  position: fixed;
  right:0;
  background-color:rgba(255,255,255,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
}

.more1{
  min-width:66px;
  height:80px;
}

.home-container{
  padding-bottom: 100px;
}

</style>
